package com.calligraphy.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.calligraphy.bases.utils.SetMsg;
import com.calligraphy.order.dto.OrderDTO;
import com.calligraphy.order.enums.ResultEnum;
import com.calligraphy.order.exception.OrderException;
import com.calligraphy.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 * 卖家端订单
 * @Author hy
 * @Date 2019/1/11 13:44
 **/
@Slf4j
@RestController
@RequestMapping("/seller/order")
public class SellerOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单列表
     * @param page 第几页，从第一页开始
     * @param size 一页有多少条数据
     * @return
     */
    @GetMapping("/list")
    @Cacheable(cacheNames = "list",key = "123")
    public JSONObject list(@RequestParam(value = "page" ,defaultValue="1") Integer page,
                           @RequestParam(value = "size",defaultValue = "10") Integer size){
        JSONObject json = new JSONObject();
        PageRequest request = PageRequest.of(page-1,size);
        Page<OrderDTO> orderDTOPage=orderService.findList(request);
        json.put("orderDTOPage",orderDTOPage);
        json.put("currentPage",page);
        json.put("size",size);
        SetMsg.setMessage(json, 0,"success");
        return json;
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @PostMapping("/cancel")
    public JSONObject cancle(@RequestParam("orderId") String orderId){
        JSONObject json = new JSONObject();
        OrderDTO orderDTO= null;
        try {
            orderDTO = orderService.findOne(orderId);
            orderService.cancel(orderDTO);
        } catch (OrderException e) {
            log.error("【卖家端取消订单】 发生异常",e);
            json.put("msg", e.getMessage());
            SetMsg.setMessage(json,1,"false");
            return json;
        }
        json.put("msg",ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
        SetMsg.setMessage(json, 0,"success");
        return json;

    }

    /**
     * 订单详情
     * @param orderId
     * @return
     */
    @PostMapping("/detail")
    public JSONObject detail(@RequestParam("orderId") String orderId){
        JSONObject json = new JSONObject();
        OrderDTO orderDTO = null;
        try{
            orderDTO = orderService.findOne(orderId);
        }catch (OrderException e){
            log.error("【卖家端查询订单详情】 发生异常{}",e);
            json.put("msg",e.getMessage());
            SetMsg.setMessage(json,1,"false");
            return json;
        }
        json.put("orderDTO",orderDTO);
        SetMsg.setMessage(json, 0,"success");
        return json;
    }

    /**
     * 完结订单
     * @param orderId
     * @return
     */
    @PostMapping("/finish")
    public JSONObject finished(@RequestParam("orderId") String orderId){
        JSONObject json = new JSONObject();
        try{
            OrderDTO orderDTO = orderService.findOne(orderId);
            orderService.finish(orderDTO);
        }catch (OrderException e){
            log.error("【卖家端完结订单】发生异常{}",e);
            json.put("msg",e.getMessage());
            SetMsg.setMessage(json,1,"false");
            return json;
        }
        json.put("msg",ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
        SetMsg.setMessage(json, 0,"success");
        return json;
    }
}
